package com.dream.mqpushserver.inteceptor;

import com.dream.mqpushserver.annotation.RpcService;
import com.dream.mqpushserver.netty.NettyServe;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 获取所有被注解标记的类 并暴露出去
 */
@Component
public class AnnotationInteceptor implements ApplicationContextAware {

    private ApplicationContext applicationContext ;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext ;

        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(RpcService.class);
        System.out.println("get bean with annotation ");
        beansWithAnnotation.forEach((k,v)->{
            Class<?>[] interfaces = v.getClass().getInterfaces();
            if (interfaces == null ){
                System.err.println(v.getClass().getName() + " is not implements interface but identifying by RpcService annotation ");
                return;
            }
            for (Class<?> clazz : interfaces){
                NettyServe.putObject(clazz.getName(), v) ;
                System.out.println(clazz.getName());
                // todo 将服务暴露出去
                // 本机ip 端口 接口全路径
            }

        });
    }
}
